package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {


    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void insert(StrategyComment strategyComment) {
        strategyCommentRepository.insert(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        //orElse方法从optional中取值，如果取不到返回null
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
//        //原始代码
//        Query query = new Query();
//        long totalcount = mongoTemplate.count(query, StrategyComment.class);
//        if(totalcount == 0){
//            //没有数据
//            return Page.empty();
//        }
//        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
//        query.with(pageable);
//        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);
//        Page<StrategyComment> page =  new PageImpl<>(list, pageable, totalcount);
//        return page;
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
            Criteria c = Criteria.where("strategyId").is(qo.getStrategyId());
            query.addCriteria(c);
        }

        return DBHelper.query(mongoTemplate, StrategyComment.class, qo, query);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        System.out.println(strategyComment.getId());
        if(!StringUtils.hasLength(strategyComment.getId())){
            //防止id为空串，直接写入到数据库，导致自动生成_id不成功
            strategyComment.setId(null);
            strategyCommentRepository.save(strategyComment);
        }
        else{
            strategyCommentRepository.save(strategyComment);
        }
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }


}
